<?php
/**
 * (logic) 状态校检（异常状态）
 */
namespace App\Logic\OverSeasIn;

use App\Models\OverSeasIn\Delivery;
use App\Models\OverSeasIn\DeliveryBox;
use App\Models\OverSeasIn\DeliveryLogistics;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;
class ReceiveExCheck
{
    /**
     * 海外仓-中转
     */
    public static function handle()
    {
        //海外仓-中转发货箱号（批次）
        DeliveryBox::query()
            ->whereHas('delivery',function (Builder $query) {
                $query->where('logistics_status', 2);
            })
            ->where('delivery_type', Delivery::OS_DELIVERY_TRANSFER) //海外仓-中转
            ->where('ex_status',1)
            ->orderBy('id')
            ->chunk(100, function ($items){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        //3：完全入库 (入库状态)
                        if($item->status == 3){
                            $item->ex_status = 2; //2：正常 (异常状态)
                            $item->save();
                        }else{
                            $dl = DeliveryLogistics::query()
                                ->where('delivery_no',$item->delivery_no)
                                ->first();
                            $days = $dl->express_channel->days ?? 0;
                            if($days){
                                $date = date('Y-m-d H:i:s',strtotime("-$days days"));
                                if($item->delivery->delivered_at < $date){
                                    $item->ex_status = 3; //3：异常 (异常状态)
                                    $item->save();
                                }
                            }
                        }
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
        });
    }

    /**
     * 海外仓-代发
     */
    public static function handle1()
    {
        //海外仓-中转发货箱号（批次）
        DeliveryBox::query()
            ->whereHas('delivery',function (Builder $query) {
                $query->where('logistics_status', 2);
            })
            ->where('delivery_type', Delivery::OS_DELIVERY_DROPSHIPPING) //海外仓-代发
            ->where('ex_status',1)
            ->orderBy('id')
            ->chunk(100, function ($items){
                DB::beginTransaction();
                try{
                    foreach ($items as $item){
                        //3：完全入库 (入库状态)
                        if($item->status == 3){
                            $item->ex_status = 2; //2：正常 (异常状态)
                            $item->save();
                        }else{
                            $dl = DeliveryLogistics::query()
                                ->where('delivery_no',$item->delivery_no)
                                ->first();
                            $days = $dl->express_channel->days ?? 0;
                            if($days){
                                $date = date('Y-m-d H:i:s',strtotime("-$days days"));
                                if($item->delivery->delivered_at < $date){
                                    $item->ex_status = 3; //3：异常 (异常状态)
                                    $item->save();
                                }
                            }
                        }
                    }
                    DB::commit();
                }catch (\Exception $e){
                    DB::rollBack();
                    report($e);
                }
            });
    }

}
